﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Zwms.Client.CommonUI;
using Zwms.Data.BusinessObjects;
using Zwms.Data.CommonObject;

namespace Zwms.Client.ManageWms
{
    public partial class CountBillSelectForm : BaseForm
    {
        #region 成员变量
        private PagerIndex _pagerIndex;
        private BilCountBill _queryRecord;
        private BilCountBill _selectedRecord;
        private CommonUI.WaitForm _waitForm = new CommonUI.WaitForm();
        #endregion

        #region 属性
        public BilCountBill SelectedRecord { get { return _selectedRecord; } }
        #endregion

        #region 构造函数
        public CountBillSelectForm()
        {
            InitializeComponent();
            this._queryRecord = new BilCountBill();
            this.InitBtnImage();
            this.tbsWarehouse.Tag = MainForm.CurrentUser.WarehouseCode;
            this.tbsWarehouse.SetTextBoxContent(MainForm.CurrentUser.WarehouseName);
            if (MainForm.CurrentUser.UserRole.Trim() == "03")
            {
                this.tbsWarehouse.SetEditState(false);
            }
            else
            {
                this.tbsWarehouse.SetEditState(true);
            }
            this.ResetDataPager();
            this.RefreshData();
        }
        #endregion

        #region 私有函数
        private void InitBtnImage()
        {
            this.btnQuery.Image = global::Zwms.Client.Properties.Resources.view;
            this.btnSelect.Image = global::Zwms.Client.Properties.Resources.ok;
            this.btnCancel.Image = global::Zwms.Client.Properties.Resources.cancel;

        }
        private void InitControl()
        {
 
        }

        private void ResetDataPager()
        {
            dataPager.PageCurrent = 1;
            dataPager.PageSize = CommonDefine.iPageSize;
        }

        private void ResetQueryRecord()
        {
            _queryRecord = new BilCountBill();
        }

        private void RefreshData()
        {
            //读入部分数据
            _pagerIndex = new PagerIndex(dataPager.PageCurrent, CommonDefine.iPageSize);
            MyDataBind(_queryRecord, _pagerIndex);
 
        }

        private void MyDataBind(BilCountBill queryRecord, PagerIndex pIndex)
        {
            try
            {
                ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
                IList<BilCountBill> queryList = client.QueryCountBillListEx(queryRecord, ref pIndex);
                LoadCountBill(queryList);
            }
            catch
            {
            }
        }

        private void LoadCountBill(IList<BilCountBill> recordList)
        {
            try 
            {
                this.Cursor = Cursors.WaitCursor;
                this.bilCountBillBindingSource.DataSource = null;
                this.bilCountBillBindingSource.DataSource = recordList;
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        #endregion

        #region 控件函数
        private void btnQuery_Click(object sender, EventArgs e)
        {
            this.RefreshData();

        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            if (dgv.CurrentCell == null)
            {
                MessageBox.Show("未选择任何记录！");
                return;
            }
            int rowIndex = dgv.CurrentCell.RowIndex;
            if (rowIndex < 0)
            {
                MessageBox.Show("未选择任何记录！");
                return;
            }
            DataGridViewRow row = dgv.Rows[rowIndex];
            this._selectedRecord = row.DataBoundItem as BilCountBill;
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void dgv_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (dgv.CurrentCell == null)
            {
                return;
            }
            int rowIndex = dgv.CurrentCell.RowIndex;
            if (rowIndex < 0)
            {
                return;
            }
            DataGridViewRow row = dgv.Rows[rowIndex];
            this._selectedRecord = row.DataBoundItem as BilCountBill;
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void tbsWarehouse_SelectButtonClick(object sender, EventArgs e)
        {
            InfoWms.WarehouseWnd uc = new InfoWms.WarehouseWnd();
            CommonUI.ContainerForm window = new CommonUI.ContainerForm(uc, "选择收货仓库");
            uc.SetQueryState();
            if (window.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                tbsWarehouse.SetTextBoxContent(uc.SelectedRecord.WarehouseName);
                tbsWarehouse.Tag = uc.SelectedRecord.Id;
            }
        }

        #endregion
    }
}
